<?php

namespace App\Http\Controllers;

use App\Article;
use App\Jobs\BlogIndexData;
use App\Tag;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function index(Request $request)
    {
        $tag = $request->get('tag');
        $data = $this->dispatch(new BlogIndexData($tag));
        $layout = $tag? Tag::layout($tag): 'blog.layouts.index';
        return view($layout, $data);
    }

    public function showArticle($slug, Request $request)
    {
        $tag = $request->get('tag');
        $article = Article::with('tags')->whereSlug($slug)->firstOrFail();
        if ($tag) {
            $tag = Tag::whereTag($tag)->firstOrFail();
        }
        return view($article->layout, compact('article', 'tag'));
    }
}
